package sg.edu.nus.iss.jass.controller;

import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.jass.constant.CommonConstants;
import sg.edu.nus.iss.jass.constant.ErrorConstants;
import sg.edu.nus.iss.jass.datastore.Transaction;
import sg.edu.nus.iss.jass.datastore.exception.DataStoreException;
import sg.edu.nus.iss.jass.dto.JobSeeker;
import sg.edu.nus.iss.jass.dto.Resume;
import sg.edu.nus.iss.jass.dto.Skill;
import sg.edu.nus.iss.jass.dto.SkillReference;
import sg.edu.nus.iss.jass.gui.DefaultActionPanel;
import sg.edu.nus.iss.jass.gui.HeaderPanel;
import sg.edu.nus.iss.jass.gui.JobSeekerFormPanel;
import sg.edu.nus.iss.jass.gui.MainFrame;
import sg.edu.nus.iss.jass.persistence.JobSeekerDAO;
import sg.edu.nus.iss.jass.persistence.ResumeDAO;
import sg.edu.nus.iss.jass.persistence.SkillDAO;
import sg.edu.nus.iss.jass.persistence.UserDAO;
import sg.edu.nus.iss.jass.privilege.JobSeekerRole;
import sg.edu.nus.iss.jass.privilege.Role;
import sg.edu.nus.iss.jass.privilege.User;
import sg.edu.nus.iss.jass.util.Util;

/**
 * <h5>JobSeekerController to manage the JobSeeker registration activity</h5>
 * This controller is called when user click on jobseeker registration menu.
 * @author Iwan Sinardinata
 */
public class JobSeekerController {

	/**
	 * mainframe that will have the mainframe.
	 */
	private MainFrame mainframe = null;
	
	/**
	 * jobSeekerDAO stores the jobseeker object.
	 */
	private JobSeekerDAO jobSeekerDAO;
	
	/**
	 * ResumeDAO stores the resume object.
	 */
	private ResumeDAO resumeDAO;
	
	/**
	 * skillDAO stores the skill object.
	 */
	private SkillDAO skillDAO;
	
	/**
	 * jobSeekerFormPanel that will have the jobseeker form panel.
	 */
	private JobSeekerFormPanel jobSeekerFormPanel = null;
/*	
	public JobSeekerController() {
		jobSeekerDAO = new JobSeekerDAO();
	} 
*/
	/**
	 * Constructor to create the JobSeekerController object.
	 * @param mainframe
	 *            reference to the container mainframe object.    
	 */	
	public JobSeekerController() {
		this.mainframe = MainController.getInstance().getMainframe();
		jobSeekerDAO = new JobSeekerDAO();
		skillDAO = new SkillDAO();
		resumeDAO = new ResumeDAO();
	}
	
	/**
	 *  Method to validate if the user id is exist.
	 * @param userID
	 *            string userID.
	 */	
	public Boolean validate(String userID) throws DataStoreException{
		Boolean validateBool = false;
		JobSeeker jobSeekerObj = jobSeekerDAO.getJobSeekerByID(String.valueOf(userID));
		if (jobSeekerObj != null) {
			validateBool = true;
		}
		return validateBool;
	}
	
	/**
	 *  Method to trigger the JobSeekerFormPanel form.
	 */	
	public void startRegisterJobSeeker() {
		jobSeekerFormPanel = new JobSeekerFormPanel(mainframe, new DefaultActionPanel(), new HeaderPanel("Register Job Seeker"));
		jobSeekerFormPanel.setJobSeekerController(this);
		jobSeekerFormPanel.setSkillsList(getAllSkills());
		jobSeekerFormPanel.show();
	}
	
	/**
	 *  Method to process registration.
	 * @param jobSeeker
	 *            JobSeeker jobSeeker object.
	 *  This method will also create one default resume  based on the Skills entered during 
	 *  Registration as we don't have any Use Case for Update JobSeeker to update or add  
	 *  Resumes for the JobSeeker.
	 */	
	public void processRegisterJobSeeker(JobSeeker jobSeeker) {
		Transaction trans = null;
		List<Skill> skillList = null;
		try {
			    trans = jobSeekerDAO.getTransaction();	
				String userID = jobSeeker.getUserId();
				Boolean jobSeekerBool = validate(userID);
				//System.out.println("jobSeekerBool:" + jobSeekerBool);
				skillList = jobSeeker.getSkills();
				if (jobSeekerBool == false) {
					trans.startTransaction();
					
					skillDAO.saveSkills(skillList.toArray(new Skill[0]));
					jobSeeker.setSkills(skillList);
					
					/* 
					 * Creating default Resume based on the Skills entered during Registration*/
					
					Resume resume = new Resume();
					resume.setResumeName("Default Resume");
					resume.setResumeDesc("Default Resume Desc");
					resume.setPersonalData(jobSeeker);
					resume.setSkillDetails((ArrayList<Skill>) skillList);
					System.out.println("resume:" + resume);
					resumeDAO.saveResume(resume);
					
					List<Resume> resumes = new ArrayList<Resume>();
					resumes.add(resume);
					jobSeeker.setResumes(resumes);
					
					jobSeekerDAO.addJobSeeker(jobSeeker);
					Role jobSeekerRole = new JobSeekerRole();
					jobSeekerRole.setId(CommonConstants.JOBSEEKER_ROLEID);
					User user = new User(jobSeeker.getUserId(), "password", jobSeekerRole);
					(new UserDAO()).addUser(user);
					
					jobSeekerFormPanel.displayCompletionMsg("The information is stored succesfully");
					trans.commit();
				} else {
					jobSeekerFormPanel.displayErrorMsg(ErrorConstants.ERROR_EXIST_USERID);
				}
			} catch(DataStoreException e) {
				//TODO handle exception or throw exception
				try {
					trans.rollback();
				} catch (DataStoreException e1) {
					e1.printStackTrace();
					jobSeekerFormPanel.displayErrorMsg(ErrorConstants.ERROR_NO_DATA);
				}
				e.printStackTrace();
			} catch(Exception e) {
				e.printStackTrace();
			//TODO handle exception or throw exception
			jobSeekerFormPanel.displayErrorMsg(ErrorConstants.ERROR_UNKNOWN);
		    }	
	}

	/**
	 *  Method to retrieve all the skill object to panel.
	 */
	private List<SkillReference> getAllSkills(){
		try {
			return skillDAO.getAllSkills();
		} catch (DataStoreException e) {
			e.printStackTrace();
			//jobSeekerFormPanel.displayErrorMsg(ErrorConstants.ERROR_RETREIVE_DATA, false);
		}
		return null;
	}
}
